package dao.impl;

import dao.IStaffManagementDao;
import pojo.Employees;
import pojo.Salaries;
import util.DBHelper;
import util.PageHelper;

import java.util.ArrayList;

public class StaffManagementDao implements IStaffManagementDao {
    DBHelper db = new DBHelper();

    //员工管理
    //员工信息添加(参数为员工对象)
    @Override
    public int insertStaff(Employees employees) {
        String sql = "insert into employees(eId,eName,positionId,departmentId,hireDate,salary,phone,email,age) values(null,?,?,?,?,?,?,?,?)";
        Object[] obj = {employees.geteName(),
                employees.getPositionId(),
                employees.getDepartmentId(),
                employees.getHireDate(),
                employees.getSalary(),
                employees.getPhone(),
                employees.getEmail(),
                employees.getAge()};
        return db.dtUpdate(sql, obj);
    }

    //单人员工信息查看
    @Override
    public ArrayList<Employees> selectStaff(Integer eId) {
        String sql = "select * from employees where eId=?";
        return db.dtQuery(sql, Employees.class, eId);
    }

    //员工信息修改(根据员工ID)
    @Override
    public int updateStaff(Employees employees) {
        String sql = "update employees set " +
                "eName=?," +
                "age=?," +
                "positionId=?," +
                "departmentId=?," +
                "hireDate=?," +
                "salary=?," +
                "phone=?," +
                "email=?" +
                " WHERE eId=?";  // 添加WHERE条件

        Object[] obj = {employees.geteName(),
                employees.getAge(),
                employees.getPositionId(),
                employees.getDepartmentId(),
                employees.getHireDate(),
                employees.getSalary(),
                employees.getPhone(),
                employees.getEmail(),
                employees.geteId()};  // 添加eId参数

        return db.dtUpdate(sql, obj);
    }

    // 员工信息列表查看(动态模糊查询,带分页)
    @Override
    public ArrayList<Employees> selectStaffs(Employees employees, PageHelper pageHelper) {
        StringBuilder sql = new StringBuilder("SELECT * FROM employees WHERE 1=1");
        ArrayList<Object> params = new ArrayList<>();

        if (employees.geteName() != null && !employees.geteName().trim().isEmpty()) {
            params.add("%" + employees.geteName() + "%");
            sql.append(" AND eName LIKE ?");
        }

        if (employees.getPositionId() != null && employees.getPositionId() != -1) {
            params.add(employees.getPositionId());
            sql.append(" AND positionId = ?");
        }

        // 添加分页
        sql.append(" LIMIT ?, ?");
        params.add(pageHelper.getStartIndex());
        params.add(pageHelper.getShowNumber());

        return db.dtQuery(sql.toString(), Employees.class, params.toArray());
    }

    // 新增：获取总记录数的方法
    @Override
    public int getTotalCount(Employees employees) {
        StringBuilder sql = new StringBuilder("SELECT COUNT(*) FROM employees WHERE 1=1");
        ArrayList<Object> params = new ArrayList<>();

        if (employees.geteName() != null && !employees.geteName().trim().isEmpty()) {
            params.add("%" + employees.geteName() + "%");
            sql.append(" AND eName LIKE ?");
        }

        if (employees.getPositionId() != null && employees.getPositionId() != -1) {
            params.add(employees.getPositionId());
            sql.append(" AND positionId = ?");
        }

        try {
            Object result = db.executeScalar(sql.toString(), params.toArray());
            return result != null ? Integer.parseInt(result.toString()) : 0;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }


    //员工信息删除(根据员工ID)
    @Override
    public int delStaff(Integer eId) {
        String sql = "delete from employees where eId=?";
        return db.dtUpdate(sql, eId);
    }

    //根据id,薪资对象修改薪资
    @Override
    public int updateSalaryById(Salaries salaries, Integer eId) {
        String sql = "update employees set salary=? where eId=?";
        Object[] objects = {(salaries.getInsurance() +
                salaries.getBenefits() +
                salaries.getBonus() +
                salaries.getBaseSalary() - salaries.getTax()), eId};
        return db.dtUpdate(sql, objects);
    }
}
